import re

from django import http
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
from django.views import View
from pymysql import DatabaseError

from apps.models import GoodsInfo


# def addGoods(request):
#     """发布商品"""
#     g = request.POST.get
#     goodsName = request.POST.get('goodsName')
#     goodsDesc = request.POST.get('goodsDesc')
#     newPrice = request.POST.get('newPrice')
#     oldPrice = request.POST.get('oldPrice')
#     authorName = request.POST.get('authorName')
#     mobile = request.POST.get('mobile')
#     print(g)
#     print(goodsName, goodsDesc, newPrice, oldPrice, authorName, mobile)
#
#     # 判断是否缺少参数
#     if not all([goodsName, goodsDesc, newPrice, oldPrice, authorName, mobile]):
#         return http.HttpResponseForbidden('缺少必传参数')
#
#     # 判断手机号是否合法
#     if not re.match(r'^1[3-9]\d{9}$', mobile):
#         return http.HttpResponseBadRequest('请输入正确的手机号码')
#
#     # 保存注册数据
#     GoodsInfo.objects.create(goodsName=goodsName, goodsDesc=goodsDesc, newPrice=newPrice, oldPrice=oldPrice, mobile=mobile, name=authorName)
#
#     return HttpResponse("添加成功")
from apps.qiniuyun import Send_qiniu


class AddGoods(View):
    """发布商品"""

    def get(self, request):
        goodss = GoodsInfo.objects.all()
        # print(goodss)
        lists = []
        for goods in goodss:
            print(goods.image)
            address_dict = {
                "id": goods.id,
                "goodsName": goods.goodsName,
                "newPrice": goods.newPrice,
                "oldPrice": goods.oldPrice,
                "imageUrl": goods.image,
            }
            lists.append(address_dict)
        return http.JsonResponse({'errmsg': '显示商品列表成功', 'success': '操作成功', 'item': lists})

    def post(self, request):
        g = request.POST.get
        goodsName = request.POST.get('goodsName')
        goodsDesc = request.POST.get('goodsDesc')
        newPrice = request.POST.get('newPrice')
        oldPrice = request.POST.get('oldPrice')
        authorName = request.POST.get('authorName')
        mobile = request.POST.get('mobile')
        # imgFilePaths = request.POST.get('imgFilePaths')
        # imgFilePaths = request.FILES.get('file')
        # print(imgFilePaths)
        print(g)
        print(goodsName, goodsDesc, newPrice, oldPrice, authorName, mobile)
        # image_url = Send_qiniu(imgFilePaths)
        # print(image_url)

        # 判断是否缺少参数
        if not all([goodsName, goodsDesc, newPrice, oldPrice, authorName, mobile]):
            return http.HttpResponseForbidden('缺少必传参数')

        # 判断手机号是否合法
        if not re.match(r'^1[3-9]\d{9}$', mobile):
            return http.HttpResponseBadRequest('请输入正确的手机号码')

        # 保存注册数据
        a = GoodsInfo.objects.create(goodsName=goodsName, goodsDesc=goodsDesc, newPrice=newPrice, oldPrice=oldPrice, mobile=mobile, name=authorName)
        b = a.id
        # return JsonResponse({'code': 1000, "result": 1, 'message': '发布成功'}, status=200)
        return http.JsonResponse({'errmsg': '发布成功', 'success': '操作成功', 'item': b})


class DetailView(View):
    """商品详情页"""

    def get(self, request):
        """提供商品详情页"""
        good_id = request.GET.get('goodsId')
        print(good_id)
        good = GoodsInfo.objects.filter(id=good_id)
        lists = []
        for goods in good:
            print(str(goods.create_time))
            time = str(goods.create_time)
            time1 = time[0:11] + time[11:16]
            # time2 = time[11:16]
            print(time1)
            address_dict = {
                "goodsId": goods.id,
                "goodsName": goods.goodsName,
                "goodsDesc": goods.goodsDesc,
                "newPrice": goods.newPrice,
                "oldPrice": goods.oldPrice,
                "goodsAuthorName": goods.name,
                "goodsCreateTime": time1,
                "goodsImageUrl": goods.image,

            }
            lists.append(address_dict)
        return http.JsonResponse({'errmsg': '显示信息成功', 'success': '操作成功', 'item': lists})


class UploadGoodsImg(View):
    """"上传照片"""

    def post(self, request):
        imgFilePaths = request.FILES.get('file')
        goodsId = request.POST.get('goodsId')
        print(goodsId)
        print(imgFilePaths)
        image_url = Send_qiniu(imgFilePaths)
        print(image_url)
        gid = GoodsInfo.objects.get(id=goodsId)
        print(gid)
        gid.image = image_url
        gid.save()

        # GoodsInfo.objects.create(image=image_url)
        return http.JsonResponse({'errmsg': '添加成功', 'success': '操作成功', 'item': 1})


